package com.yyge.config;

import com.yyge.interceptor.GlobalInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class SpringMvcConfig implements WebMvcConfigurer {

    @Autowired
    @Qualifier("globalInterceptor")
    private GlobalInterceptor globalInterceptor;

    @Autowired
    private MyInterceptor myInterceptor;

    /**
     * 跨域
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedHeaders("*")
                .allowedMethods("*")
                .allowCredentials(false)
                .maxAge(1800);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 多个拦截器的执行顺序addInterceptor顺序执行
        registry.addInterceptor(globalInterceptor)
                .addPathPatterns("/*");
        registry.addInterceptor(myInterceptor)
                .addPathPatterns("/*");
    }
}